package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    //班级列表查询-分页查询
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        log.info("分页查询班级列表信息:{}",clazzQueryParam);
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    @Log
    //删除班级
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据ID删除班级:{}",id);
        clazzService.deleteById(id);
        return Result.success();
    }

    @Log
    //添加班级
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        log.info("添加班级");
        clazzService.addClazz(clazz);
        return Result.success();
    }

    //根据ID查询班级
    @GetMapping("/{id}")
    public Result getClazzById(@PathVariable Integer id){
        log.info("根据ID查询班级:{}",id);
        Clazz clazz = clazzService.getClazzById(id);
        if (clazz == null){
            return Result.error("班级不存在");
        }
        return Result.success(clazz);
    }

    @Log
    //修改班级
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级:{}",clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    //查询所有班级
    @GetMapping("/list")
    public Result findAll(){
        log.info("查询所有班级");
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }
}
